package com.xiaodu.acer.phoneplay.common.rx.subscriber;

import android.content.Context;
import android.content.Intent;

import com.orhanobut.logger.Logger;
import com.xiaodu.acer.phoneplay.common.exception.BaseException;
import com.xiaodu.acer.phoneplay.common.rx.RxErrorHandler;
import com.xiaodu.acer.phoneplay.ui.activity.LoginActivity;

import io.reactivex.disposables.Disposable;


public abstract class ErrorHandlerSubscriber<T> extends DefualtSubscriber<T> {


    protected RxErrorHandler mErrorHandler = null;

    protected Context mContext;

    public ErrorHandlerSubscriber(Context context) {

        this.mContext = context;

        mErrorHandler = new RxErrorHandler(mContext);

    }

    @Override
    public void onSubscribe(Disposable d) {

    }


    @Override
    public void onError(Throwable e) {
        BaseException baseException = mErrorHandler.handleError(e);

        if (baseException == null) {
            e.printStackTrace();
            Logger.d("ErrorHandlerSubscriber", e.getMessage());
        } else {
            mErrorHandler.showErrorMessage(baseException);
            //如果是token错误，重新去登录页面
            if (baseException.getCode() == BaseException.ERROR_TOKEN) {
                toLogin();
            }

        }

    }

    private void toLogin() {
        Intent intent = new Intent(mContext, LoginActivity.class);
        mContext.startActivity(intent);
    }


}
